package com.bsh.erp.pro.base.action;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.junit.Test;
import org.springframework.context.ApplicationContext;

import com.alibaba.fastjson.JSON;
import com.bsh.erp.base.BaseAction;
import com.bsh.erp.entity.User;
import com.bsh.erp.service.UserService;
import com.bsh.erp.tools.DateUtil;
import com.bsh.erp.tools.MD5;
import com.opensymphony.xwork2.ActionContext;

/**
 * 实现了 用户登录操作等对用户表的操作
 * @author JiaQi
 */
public class UserAction extends BaseAction<User>{
	private String account;//接受页面发送 账套
	public void setAccount(String account) {this.account = account;}
	private String checkCode; //接受验证码
	public void setCheckCode(String checkCode) {this.checkCode = checkCode;}

	/**
	 * 判定登录操作,如果登录成功将登录用户放置Session中
	 * 登录失败，放置提示信息
	 * toUserLogin 登录失败
	 * toIndex 登录成功
	 * @author JiaQi
	 * @return 返回命令，跳转页面
	 */
	public void UserLogin(){
//		获取数据
		User user = userService.findEntityByHQL("from User where userName = ? and password = ?",getModel().getUserName(),getModel().getPassword());
//		登录判定
		if(user == null){
//			登录失败
			outJson("{style:001}");
//			登录失败
		}else if(user != null){
			String token = MD5.MD5Encode(user.getUserName()+DateUtil.getUUID());
			outJson(token+","+JSON.toJSONString(user));
			ActionContext.getContext().getSession().put(token, user);
		    for (Entry<String, Object> entry : ActionContext.getContext().getSession().entrySet()) {
		        System.out.println(entry.getKey() + ":" + entry.getValue());
		      }
		}
	}
	
	/**
	 * 设置当前账套，将账套放置当前Session中
	 * toIndex 系统首页
	 * @author JiaQi
	 * @return 跳转系统首页
	 */
	public String setAccount(){
//		放置Session
		setAccount(account);
		return "toIndex";
	}
}
